package com.sqfw.project.wp.controller;

import com.alibaba.excel.EasyExcel;
import com.sqfw.common.utils.file.FileUtils;
import com.sqfw.project.utils.TestFileUtil;
import com.sqfw.project.wp.domain.vo.PrintWpInfoVO;
import com.sqfw.project.wp.service.IWpInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @ Author：linchangchun
 * @ Date：2023-12-16-11:45
 * @ Version：1.0
 */
@Slf4j
@RestController
@RequestMapping("/excelDownLoad")
public class ExcelDownLoadController {

    @Autowired
    private IWpInfoService wpInfoService;

    /**
     * 打印导出工单接口
     * */
    @PreAuthorize("@ss.hasPermi('downLoadExcel')")
    @GetMapping("/downLoadExcel")
    public void downLoadExcel(String scid,HttpServletResponse response, HttpServletRequest request) {

        PrintWpInfoVO printWpInfoVO = wpInfoService.getPrintWpInfo(scid);

        String templateFileName = System.getProperty("user.dir") + "/file/" + "downLoadTemplate.xlsx";

        String fileName = TestFileUtil.getFileDir("down") + System.currentTimeMillis() + ".xlsx";

        EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(printWpInfoVO);


        try
        {

            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            UUID uuid = UUID.randomUUID();
            String replace = uuid.toString().replace("-", "");
            FileUtils.setAttachmentResponseHeader(response, replace+".xlsx");
            FileUtils.writeBytes(fileName, response.getOutputStream());

        }
        catch (Exception e)
        {
            log.error("下载文件失败", e);
        }finally {
            FileUtils.deleteFile(fileName);
        }



    }


}
